package com.settlement.system.handler;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NC凭证汇总导出加批注
 * @author user
 */
public class TblNcVurcherSummaryCommentWriteHandler  extends AbstractRowWriteHandler {

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                Integer relativeRowIndex, Boolean isHead) {
        if (row.getRowNum()==1) {
            Sheet sheet = writeSheetHolder.getSheet();
            {
                List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
                for (int i=0;i<mergedRegions.size();i++) {
                    sheet.removeMergedRegion(i);
                }
            }
            Drawing<?> drawingPatriarch = sheet.createDrawingPatriarch();

            Map<String,String> map = new HashMap<String,String>(16);
            map.put("核算账簿","103060704-0001 滔搏\n" +
                    "10904-0001 百丽\n" +
                    "10905-0001 优购\n" +
                    "102060105-0001 百丽鞋业\n" +
                    "10915005-0001 法迅\n" +
                    "10911005-0001 巴罗克\n" +
                    "10916005-0001 依迪索\n" +
                    "10933006-0001 凡尚\n");
            map.put("凭证类别编码","01  记账凭证\n" +
                    "02  收款凭证\n");
            map.put("附单据数","默认值：0\n");
            map.put("币种","默认值：人民币\n");
            map.put("业务单元编码","B列，'-' 前面的值\n");
            map.put("业务日期","与G列的制单日期一致\n");
            map.put("组织本币汇率","默认值：1\n");
            System.out.println(row.getFirstCellNum());
            System.out.println(row.getLastCellNum());
            for(int i=1;i<=row.getLastCellNum();i++){
                Cell cell = row.getCell(i);
                if(cell==null){
                    continue;
                }
                String key = cell.getStringCellValue();
                String value = map.get(key);
                if(StringUtils.isNotBlank(value)){
                    Comment comment =
                            drawingPatriarch.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short)1, 0, (short)2, 1));

                    comment.setString(new XSSFRichTextString(value));
                    cell.setCellComment(comment);
                }
            }
        }


    }

}